package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"xagent/internal/providers"
	"xagent/internal/streaming"
)

func main() {
	config := &providers.ProviderConfig{
		Type:   "openai",
		APIKey: os.Getenv("OPENAI_API_KEY"),
		Model:  "gpt-3.5-turbo",
	}

	provider, err := providers.NewOpenAIProvider(config)
	if err != nil {
		log.Fatal(err)
	}

	messages := []providers.Message{
		{Role: "user", Content: "Tell me a story about a brave knight"},
	}

	// 使用自定义处理器
	handler := &streaming.DefaultStreamHandler{
		OnEventFunc: func(event *streaming.StreamEvent) error {
			fmt.Print(event.Content)
			return nil
		},
		OnCompleteFunc: func(ctx context.Context) error {
			fmt.Println("\n--- Story complete ---")
			return nil
		},
		OnErrorFunc: func(ctx context.Context, err error) error {
			fmt.Printf("\nError: %v\n", err)
			return nil
		},
	}

	ctx := context.Background()
	if err := provider.StreamChatWithHandler(ctx, messages, nil, handler); err != nil {
		log.Fatal(err)
	}

	// 或者使用原始的流式接口
	stream, err := provider.StreamChat(ctx, messages, nil)
	if err != nil {
		log.Fatal(err)
	}

	for event := range stream {
		if event.Error != nil {
			log.Fatal(event.Error)
		}
		fmt.Print(event.Content)
		if event.Done {
			fmt.Println("\n--- Story complete ---")
		}
	}
}
